<template>
  <view class="payCenter-page">
    <!-- tabs -->
    <view>
      <u-tabs
        :list="list"
        :is-scroll="false"
        :current="current"
        @change="change"
        bar-width="130"
        duration=".3"
        active-color="#19b1b8"
        font-size="28"
      ></u-tabs>
    </view>
    <!-- 申请时间 -->
    <view class="time-view fs-24 colo-6">
      <view class="note-view">
        <text>申请提现时间：</text>
        <view @click="open">
          <image class="note-img" src="@/static/image/note.png"></image>
          <text class="note-btn">提现规则</text>
        </view>
      </view>

      北京时间每月
      <text class="time-txt fs-24">16-20日</text>
      ，每条数据每月只可提现一次
    </view>
    <!-- 详情 -->
    <view class="pay-info">
      <payContent :payData="payData" :isLogin="true"></payContent>
    </view>
    <!--  -->
    <button class="pay-btn" @click="goToPayApply">申请提现</button>
    <u-modal
      v-model="show"
      @confirm="confirm"
      :title="title"
      :content="content"
      :show-cancel-button="false"
      confirm-text="知道了"
      :confirm-style="{ fontSize: '36rpx', color: '#19b1b8' }"
      :title-style="{
        fontSize: '36rpx',
        color: '#333!important',
        fontWeight: '400',
      }"
      :content-style="{
        fontsize: '30rpx',
        color: '#666',
        lineHeight: '48rpx',
        textAlign: 'left!important',
    }"
    >
      <view class="slot-content">
        <rich-text :nodes="content"></rich-text>
      </view>
    </u-modal>
    <u-toast ref="uToast" />
  </view>
</template>

<script>
import { getAccountAmount, getjudge } from "@/api/user";
import payContent from "../components/payContent";
export default {
  components: {
    payContent,
  },
  data() {
    return {
      list: [],
      current: 0,
      isLogin: true,
      show: false, // 弹框
      title: "提现规则",
      content:
        "1、每月只可申请一次<br/>2、译员所得收入会按照国家标准进行扣税<br/>3、到账时间：当月月底前（节假日顺延）", //弹框内容
      payData: "",
      CNY: {
        totalRev: 0, // 总收入
        balance: 0, // 余额
        withdrawable: 0, // 可提现
        num: 0,
      }, //人民币
      USD: {
        totalRev: 0,
        balance: 0,
        withdrawable: 0,
        num: 0,
      }, //美元
      EUR: {
        totalRev: 0,
        balance: 0,
        withdrawable: 0,
        num: 0,
      }, //欧元
      GBP: {
        totalRev: 0,
        balance: 0,
        withdrawable: 0,
        num: 0,
      }, //英镑

      judge: {}, // 提现条件
    };
  },
  methods: {
    // ----------------------------------提现--------------------------------------------

    // 提现
    goToPayApply() {
      // 首先判断，是否在提现时间内
      if (!this.judge.currentDayRequire) {
        this.$refs.uToast.show({
          title: "当前不在提现时间内",
          type: "warning",
        });
        return null;
      }
      // 未身份认证
      if (!this.$store.state.userData["isPassIdentity"]) {
        this.$refs.uToast.show({
          title: "请先进行身份认证",
          type: "warning",
        });
        return null;
      }
      // 未财务认证
      if (!this.$store.state.userData["isPassFinance"]) {
        this.$refs.uToast.show({
          title: "请先进行结算方式设置",
          type: "warning",
        });
        return null;
      }
      // 判断币种及金额
      let cashTypeEl = {};
      switch (this.list[this.current].name) {
        case "人民币":
          cashTypeEl = {
            type: "CNY",
            typeName: "人民币",
            total: this.CNY.totalRev,
          };
          // if (this.CNY.num - 0 <= 0) {
          //   this.$refs.uToast.show({
          //     title: "可提现金额不足",
          //     type: "warning",
          //   });
          //   return null;
          // }
          break;
        case "美元":
          cashTypeEl = {
            type: "USD",
            typeName: "美元",
            total: this.USD.totalRev,
          };
          // if (this.USD.num - 0 <= 0) {
          //   this.$refs.uToast.show({
          //     title: "可提现金额不足",
          //     type: "warning",
          //   });
          //   return null;
          // }
          break;
        case "欧元":
          cashTypeEl = {
            type: "EUR",
            typeName: "欧元",
            total: this.EUR.totalRev,
          };
          // if (this.EUR.num - 0 <= 0) {
          //   this.$refs.uToast.show({
          //     title: "可提现金额不足",
          //     type: "warning",
          //   });
          //   return null;
          // }
          break;
        case "英镑":
          cashTypeEl = {
            type: "GBP",
            typeName: "英镑",
            total: this.GBP.totalRev,
          };
          // if (this.GBP.num - 0 <= 0) {
          //   this.$refs.uToast.show({
          //     title: "可提现金额不足",
          //     type: "warning",
          //   });
          //   return null;
          // }
          break;
      }
      const stateMap = this.judge.cashStateHashMap,
        tarCls = cashTypeEl.type;
      console.log(stateMap, tarCls, stateMap[tarCls]);
      //再判断每个币种，是否还有提现未完成的操作
      if (Object.keys(stateMap || {}).length === 0) {
        //若不存在，无需判断
      } else {
        //若存在，判断处于提现状态
        if (stateMap[tarCls] >= 0) {
          switch (stateMap[tarCls]) {
            case 0:
              this.$refs.uToast.show({
                title: "提现申请已提交，请静待财务审核",
                type: "warning",
              });
              return null;
            case 1:
            case 3:
              this.$refs.uToast.show({
                title: "财务审核已通过，请静待放款",
                type: "warning",
              });
              return null;
            case 2:
              this.$refs.uToast.show({
                title: "每个币种每月只能提现一次哟，下个月再来吧",
                type: "warning",
              });
              return null;
          }
        }
      }

      const judge = this.getJudgeResult(cashTypeEl), //获取结算类型，社保51等
        judgeInfo = this.judge, //提现条件，比如信息完善情况等
        result = judge.result.toLowerCase(); //把结算类型转换为小写
      // console.log(
      //   "judge=",
      //   judgeInfo.billingType,
      //   judgeInfo,
      //   cashTypeEl,
      //   result
      // );
      switch (result) {
        case "paypal":
        case "国外银行":
          if (!judgeInfo.paypal && !judgeInfo.overseasBank) {
            //judgeInfo.paypal=false,没有PayPal信息；judgeInfo.overseasBank=false，没有国外银行信息
            this.$refs.uToast.show({
              title: "请先完善PayPal/国外银行信息",
              type: "warning",
            });
            return false;
          }
          if (result === "paypal" && !judgeInfo.paypal) {
            this.$refs.uToast.show({
              title: "请先完善PayPal信息",
              type: "warning",
            });
            return false;
          }
          if (result === "国外银行" && !judgeInfo.overseasBank) {
            this.$refs.uToast.show({
              title: "请先完善国外银行信息",
              type: "warning",
            });
            return false;
          }
          if (result === "paypal") {
            judgeInfo.overseasBank = false;
          }
          if (result === "国外银行") {
            judgeInfo.paypal = false;
          }
          this.subinfor(1, result, cashTypeEl);
          break;
        case "社保51":
          if (!judgeInfo.alipay && !judgeInfo.bank) {
            this.$refs.uToast.show({
              title: "请先完善银行卡信息",
              type: "warning",
            });
            return false;
          }
          this.subinfor(2, result, cashTypeEl);
          break;
        case "云账户":
          if (!judgeInfo.bank) {
            this.$refs.uToast.show({
              title: "请先完善银行卡信息",
              type: "warning",
            });
            return false;
          }
          this.subinfor(3, result, cashTypeEl);
          break;
        case "非全日制":
          if (!judgeInfo.bank) {
            this.$refs.uToast.show({
              title: "请先完善银行卡信息",
              type: "warning",
            });
            return false;
          }
          this.subinfor(4, result, cashTypeEl);
          break;
        case "校企合作":
          if (!judgeInfo.bank) {
            this.$refs.uToast.show({
              title: "请先完善银行卡信息",
              type: "warning",
            });
            return false;
          }
          this.subinfor(5, result, cashTypeEl);
          break;
      }
    },
    // 提现条件：是否为大陆，是否进行身份认证，身份认证方式，结算币种，是否在18-60之间，是否为首次提现，是否进行结算认证，结算方式以及当前默认结算方式
    getjudge() {
      getjudge().then((res) => {
        if (res.message === "success") {
          this.judge = res.data;
        } else {
          this.$refs.uToast.show({
            title: res.message,
            type: "error",
          });
        }
      });
    },
    // 跳转提现详情，确认提现信息 type：1、paypal和国外银行卡， 2、社保51，3、云账户，4、非全日制，5、校企合作
    subinfor(type, result, cashTypeEl) {
      console.log('lllllllll=',result)
      uni.navigateTo({
        url:
          "/pages/user/payApply/index?type=" +
          type +
          "&result=" +
          result +
          "&cashTypeEl=" +
          cashTypeEl.type,
      });
    },
    // 获取结算类型
    getJudgeResult(cashTypeEl) {
      const judge = this.judge; //获取提现条件
      const payType = judge.billingType.toLowerCase();

      //团队，默认校企合作，否则为设定类型
      if (this.$store.state.userData["USER_TYPE"] === "1") {
        if (cashTypeEl.type === "CNY") {
          //人民币
          if (!payType) {
            //走流程
            return {
              result: "校企合作",
            };
          } else {
            return {
              result: payType,
            };
          }
        } else {
          return {
            result: payType.indexOf("国外银行") >= 0 ? payType : "PayPal",
          };
        }
      } else {
        //个人（非团队），默认走流程，否则为设定类型
        if (!payType) {
          //走流程
          if (judge.mainlandOrNot && judge.certificateType === "身份证") {
            //大陆
            if (cashTypeEl.type === "CNY") {
              //人民币结算
              if (judge.ageReuire) {
                //18-60周岁
                return {
                  result: "社保51",
                };
              } else {
                //非18-60周岁
                return {
                  result: "云账户",
                };
              }
            } else {
              //非人民币结算
              return {
                result: "PayPal",
              };
            }
          } else {
            //非大陆
            if (cashTypeEl.type === "CNY") {
              //人民币结算
              return {
                result: "云账户",
              };
            } else {
              //非人民币结算
              return {
                result: "PayPal",
              };
            }
          }
        } else {
          //设定类型
          if (cashTypeEl.type === "CNY") {
            //人民币
            return {
              result: payType,
            };
          } else {
            //外币（非人民币）
            return {
              result: payType.indexOf("国外银行") >= 0 ? payType : "PayPal",
            };
          }
        }
      }
    },

    // ------------------------------------其他------------------------------------------
    change(index) {
      this.current = index;
      this.payData = this.list[index].data;
    },
    open() {
      this.show = true;
    },
    confirm() {
      this.show = false;
    },
    // 获取账户金额
    getAccountAmount() {
      let userCode = this.$store.state.userData["userCode"];
      getAccountAmount(userCode).then((res) => {
        if (res.message === "success") {
          const data = res.data;
          if (data.cNY_Balance) {
            this.CNY.totalRev = "￥" + data.cNY_TotalIncome;
            this.CNY.balance = "￥" + data.cNY_Balance;
            this.CNY.withdrawable = "￥" + data.cNY_Withdrawable;
            this.CNY.num = data.cNY_Withdrawable;
            this.list.push({ name: "人民币", data: this.CNY });
          }
          if (data.uSD_Balance) {
            this.USD.totalRev = "$" + data.uSD_TotalIncome;
            this.USD.balance = "$" + data.uSD_Balance;
            this.USD.withdrawable = "$" + data.uSD_Withdrawable;
            this.USD.num = data.uSD_Withdrawable;
            this.list.push({ name: "美元", data: this.USD });
          }
          if (data.eUR_Balance) {
            this.EUR.totalRev = "€" + data.eUR_TotalIncome;
            this.EUR.balance = "€" + data.eUR_Balance;
            this.EUR.withdrawable = "€" + data.eUR_Withdrawable;
            this.EUR.num = data.eUR_Withdrawable;
            this.list.push({ name: "欧元", data: this.EUR });
          }
          if (data.gBP_Balance) {
            this.GBP.totalRev = "￡" + data.gBP_TotalIncome;
            this.GBP.balance = "￡" + data.gBP_Balance;
            this.GBP.withdrawable = "￡" + data.gBP_Withdrawable;
            this.GBP.num = data.gBP_Withdrawable;
            this.list.push({ name: "英镑", data: this.GBP });
          }
          this.payData = this.list[0].data;
        } else {
          this.$refs.uToast.show({
            title: res.message,
            type: "error",
          });
        }
      });
    },
    // 提现按钮触发事件
  },
  onShow() {
    this.list = [];
    this.current = 0;
    this.getAccountAmount();
    // 获取提现条件
    this.getjudge();
  },
};
</script>

<style lang="scss">
</style>
